import { onMounted, reactive, ref, nextTick } from "vue";
import { listOrderParm } from "@/api/order/OrderModel";
import { getOrderListApi, sendOrderApi } from "@/api/order";
import useInstance from "@/tools/useInstance";

export default function useOrderTable() {
    const { global } = useInstance()
    //表格高度
    const tableHeight = ref(0)
    //表格数据
    const tableList = ref([])
    //列表查询的参数
    const listParm = reactive<listOrderParm>({
        page: 1,
        pageSize: 10,
        totalSize: 0,
        userName: '',
        phone: '',
        status:'',
    })
    //列表
    const getList = async () => {
        let result = await getOrderListApi(listParm)
        if (result && result.code == 200) {
            //设置表格数据
            tableList.value = result.data.data
            //设置分页总条数
            listParm.totalSize = result.data.totalPage
        }
    }
    //搜索
    const searchBtn = () => {
        getList()
    }
    //重置
    const resetBtn = () => {
        listParm.page = 1
        listParm.phone = ''
        listParm.userName = ''
        listParm.status = ''
        getList()
    }
    //总页数改变时触发
    const sizeChange = (size: number) => {
        listParm.pageSize = size;
        getList()
    }
    //当前页数改变时触发
    const currentChange = (page: number) => {
        listParm.page = page;
        getList()
    }
    //发货
    const sendOrder = async (orderId: string) => {
        let flag = null
        let result = await sendOrderApi(orderId)
        if(result &&result.code == 1){
            flag = await global.$messageBox(result.msg)
        }else{
            flag = await global.$messageBox('确定发货吗')
            if (flag) {
                if (result && result.code == 200) {
                    getList()
                }
            }
        }
    }
    onMounted(() => {
        getList()
        //计算表格高度
        nextTick(() => {
            tableHeight.value = window.innerHeight - 435
        })
    })
    return {
        listParm,
        getList,
        searchBtn,
        resetBtn,
        tableList,
        sizeChange,
        currentChange,
        tableHeight,
        sendOrder
    }
}